package dao.impl;

import dao.i.ContentDao;
import entity.Content;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ContentDaoImpl implements ContentDao {

    Connection connection = helper.DataSource.getConnection();

    @Override
    public int selectContentAllCount(int brandId) {
        String sql = "select count(*) from content where brandId=" + brandId;
        Statement statement = null;
        ResultSet resultSet = null;
        int num = 0;
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            resultSet.next();
            num = resultSet.getInt(1);
            return num;
        } catch (SQLException ex) {
            Logger.getLogger(ContentDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            try {
                statement.close();
            } catch (SQLException ex1) {
                Logger.getLogger(ContentDaoImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
            try {
                resultSet.close();
            } catch (SQLException ex1) {
                Logger.getLogger(ContentDaoImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        return num;
    }

    @Override
    public List<Content> selectContentByBrandId(int brandId) {
        String sql = "select content.*,userInfo.userName from Content,userInfo where content.userInfoId=userInfo.id and brandId=" + brandId;
        List<Content> listContent = new ArrayList<>();
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            System.out.print(resultSet.getRow());
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String title = resultSet.getString("title");
                String body = resultSet.getString("body");
                int brandId1 = resultSet.getInt("brandId");
                int userInfoId = resultSet.getInt("userInfoId");
                String userName = resultSet.getString("userName");
                String putTime = resultSet.getString("putTime");
                Content content = new Content(id, title, body, brandId1, userInfoId, userName, putTime);
                listContent.add(content);
            }
            return listContent;
        } catch (SQLException ex) {
            ex.printStackTrace();
            try {
                statement.close();
            } catch (SQLException ex1) {
                ex1.printStackTrace();
            }
            try {
                resultSet.close();
            } catch (SQLException ex1) {
                ex1.printStackTrace();
            }
        }
        return null;
    }

     //按id获取内容
    @Override
    public Content selectContentdById(int id) {
        String sql = "select content.*,userInfo.userName from Content,userInfo where content.userInfoId=userInfo.id and content.id=" + id;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            System.out.print(resultSet.getRow());
            resultSet.next();
            int ids = resultSet.getInt("id");
            String title = resultSet.getString("title");
            String body = resultSet.getString("body");
            int brandId1 = resultSet.getInt("brandId");
            int userInfoId = resultSet.getInt("userInfoId");
            String userName = resultSet.getString("userName");
            String putTime = resultSet.getString("putTime");
            Content content = new Content(id, title, body, brandId1, userInfoId, userName, putTime);
            return content;
        } catch (SQLException ex) {
            ex.printStackTrace();
            try {
                statement.close();
            } catch (SQLException ex1) {
                ex1.printStackTrace();
            }
            try {
                resultSet.close();
            } catch (SQLException ex1) {
                ex1.printStackTrace();
            }
        }
        return null;
    }

}
